/*
* 主角;
*/
class SelfPlayer{
    /**2D显示对象 */
    protected _disObj:Sprite;
    /**3D显示对象 */
    private _disObj3d:Sprite3D;
    private static _instance:SelfPlayer;
    private _tween:Laya.Tween;
    private constructor(){
        this._disObj = new Sprite();
        let sp:Sprite = Sprite.fromImage("res/map/player.png");
        this._disObj.addChild(sp);
        this._disObj.pivot(48,96);
        this.create3dObj();
    }

    public static get instance():SelfPlayer
    {
        if(!this._instance)
        {
            this._instance = new SelfPlayer();
        }
        return this._instance;
    }

    private create3dObj():void
	{
        this._disObj3d = Sprite3D.load("res/models/cike/cike.lh");
        SceneManager.instance.container3d.addChild(this._disObj3d);
        this._disObj3d.once(Laya.Event.HIERARCHY_LOADED, this, function():void
        {
            this._disObj3d.transform.localScale = new Vector3(0.1, 0.1, 0.1);
        });			
	}

    public update():void
    {
        if(this._disObj && this._disObj3d)
        {
            var twoPos:Vector3 = this.getGlobalVec3();
            var threePos:Vector3 = new Vector3();
            Tool.screenCoordTo3DCoord(twoPos, threePos);
            this._disObj3d.transform.position = threePos;
        }
    }

    public getGlobalVec3():Vector3
    {
        var pos:Vector3 = new Vector3();
        if(this._disObj)
        {
            var gp:Laya.Point = SceneManager.instance.scene.localToGlobal(new Laya.Point(this._disObj.x, this._disObj.y));
            pos.x = gp.x;
            pos.y = gp.y;
        }
        return pos;
	}

    /**
     * 显示对象
     */
    public get disObj() : Sprite {
        return this._disObj;
    }
    
    public moveTo(point:Laya.Point):void
    {
        if (this._tween)  {
            this._tween.clear();
        }
        this._tween = Laya.Tween.to(this._disObj, {
            x: point.x,
            y: point.y
        }, 1000)
    }
}